package com.tiantian.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tiantian.bean.PageBean;
import com.tiantian.dao.BuyerRaiseCashDetailsRepository;
import com.tiantian.datajpa.service.impl.GenericServiceImpl;
import com.tiantian.domain.Buyerraisecashdetails;
import com.tiantian.domain.BuyerraisecashdetailsId;
import com.tiantian.service.BuyerRaiseCashDetailsService;
import com.tiantian.util.CommonConstant.QueryType;
import com.tiantian.util.ConditionResult;

@Service
@Transactional
public class BuyerRaiseCashDetailsServiceImpl extends
		GenericServiceImpl<Buyerraisecashdetails, BuyerraisecashdetailsId> implements BuyerRaiseCashDetailsService {
	private BuyerRaiseCashDetailsRepository repository;

	@Autowired
	public void setRepository(BuyerRaiseCashDetailsRepository repository) {
		super.setGenericDAO(repository);
		this.repository = repository;
	}

	/**
	 * 获取买手体现详情信息
	 * 
	 * @param params
	 * @param pageBean
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getRaiseCashList(
			Map<String, String> params, PageBean pageBean) {
		ConditionResult conditionResult = getQueryCondition(params,
				QueryType.LIST);
		List<Map<String, Object>> ret = repository.queryHumpListMapBySql(
				conditionResult.getSql(), pageBean.getStartIndex(),
				pageBean.getPageSize(), conditionResult.getParams());
		conditionResult = getQueryCondition(params, QueryType.COUNT);
		pageBean.setTotal(repository.queryCountBySql(conditionResult.getSql(),
				conditionResult.getParams()));
		return ret;
	}

	private ConditionResult getQueryCondition(Map<String, String> searchMap,
			QueryType type) {
		StringBuilder sql = new StringBuilder();
		Map<String, Object> params = new HashMap<String, Object>();

		switch (type) {
		case LIST:
			sql.append(" select a.* ,d.areaname,c.bankname,b.CardholderName,b.BankCardAccount,e.bankname accountBankname ");
			
			break;
		case COUNT:
			sql.append(" select count(1) ");
			break;
		default:
			break;
		}
		sql.append("from BuyerRaiseCashDetails a ");
		sql.append("left join BuyersBankCard b on a.BankCardID=b.BankCardID ");
		sql.append("left join BankList c on c.BankID=b.BankName ");
		sql.append("left join RegionalInformation d on d.AreaCode=b.city ");
		sql.append("left join BankList e on c.BankID=b.AccountBank ");
		sql.append("  where 1=1 ");

		if (searchMap != null) {

			if (StringUtils.isNotBlank(searchMap.get("statusInfor"))) {
				sql.append(" and a.statusInfor = :statusInfor ");
				params.put("statusInfor", searchMap.get("statusInfor").trim());
			}
			if (StringUtils.isNotBlank(searchMap.get("buyersUserID"))) {
				sql.append(" and a.buyersUserID like :buyersUserID ");
				params.put("buyersUserID",
						"%" + searchMap.get("buyersUserID").trim() + "%");
			}
			if (StringUtils.isNotBlank(searchMap.get("minMoneyAmout"))) {
				sql.append(" and a.MoneyAmout >= :minMoneyAmout ");
				params.put("minMoneyAmout",
						searchMap.get("minMoneyAmout"));
			}
			if (StringUtils.isNotBlank(searchMap.get("maxMoneyAmout"))) {
				sql.append(" and a.MoneyAmout <= :maxMoneyAmout ");
				params.put("maxMoneyAmout",
						searchMap.get("maxMoneyAmout"));
			}
			if (StringUtils.isNotBlank(searchMap
					.get("startRaiseCashTime"))) {
				sql.append(" and a.RaiseCashTime >= :startRaiseCashTime ");
				params.put("startRaiseCashTime",
						searchMap.get("startRaiseCashTime"));
			}
			if (StringUtils.isNotBlank(searchMap
					.get("endRaiseCashTime"))) {
				sql.append(" and  a.RaiseCashTime <= :endRaiseCashTime ");
				params.put("endRaiseCashTime",
						searchMap.get("endRaiseCashTime"));
			}
			
			if (StringUtils.isNotBlank(searchMap.get("bankName"))) {
				sql.append(" and c.bankName like :bankName ");
				params.put("bankName",
						"%" + searchMap.get("bankName").trim() + "%");
			}
			
			if (StringUtils.isNotBlank(searchMap.get("areaName"))) {
				sql.append(" and d.areaName like :areaName ");
				params.put("areaName",
						"%" + searchMap.get("areaName").trim() + "%");
			}
		}
		if (type == QueryType.LIST) {
			sql.append(" order by a.RaiseCashTime desc");
		}
		return new ConditionResult(sql.toString(), params);

	}
	@Override
	public List<Buyerraisecashdetails> getBuyerRaiseCashDetails(
			Map<String, String> param) {
		String[] ids = StringUtils.split(param.get("buyersUserID"), ",");
		String[] times = StringUtils.split(
				param.get("raiseCashTime"), ",");
		Map<String, Object> params = new HashMap<>();
		String temp = "select * from  BuyerRaiseCashDetails  where ";
		for (int i = 0; i < ids.length; i++) {

			String id = ids[i];
			String time = times[i];
			temp += " ( buyersUserID=:id_" + i;
			temp += " and UNIX_TIMESTAMP(raiseCashTime)*1000=:time_"
					+ i + " )";
			if (i != ids.length - 1) {
				temp += " or ";
			}
			params.put("id_" + i, id);
			params.put("time_" + i, time);
		}
		return repository.queryListBySql(temp, params,
				Buyerraisecashdetails.class);

	}
	@Override
	public Long count(Map<String, String> params) {
		ConditionResult conditionResult = getQueryCondition(params, QueryType.COUNT);
		return repository.queryCountBySql(conditionResult.getSql(), conditionResult.getParams());
	}

	
}
